import { defineStore } from 'pinia'
import request from '@/utils/request.js'
import downloadFile from '@/utils/downloadFile.js'
import axios from 'axios'
import {
  editWarehouse,
  addWarehouse,
  delWarehouse,
  editMaterial,
  addMaterial,
  delMaterial,
  export_excel
} from '@/service/warehouse/index.js'
import responseMessage from '@/utils/response.js'

const useApiStore = defineStore('warehouse', {
  state: () => ({
    deptTree: '',
    taskStatus: '1',
    queryList: null,
    page: {
      pageNum: 1,
      pageSize: 10,
      total: 40
    }
  }),
  actions: {
    /* 仓库管理 */
    //获取仓库列表
    async getQueryList(params) {
      const res = await request.get('/queryMainTasks', {
        params
      })
      this.queryList = res.data
      return res.data
    },
    //添加仓库信息
    async addWarehouseHandler(addForm) {
      const res = await addWarehouse(addForm)
      responseMessage(res)
    },
    //编辑仓库信息
    async editWarehouseHandler(addForm) {
      const res = await editWarehouse(addForm)
      responseMessage(res)
    },
    //删除仓库信息
    async delWarehouseHandler(id) {
      const res = await delWarehouse(id)
      responseMessage(res)
    },
    async downResultExcelFile(params, fileName) {
      const request = axios.create({
        baseURL: 'http://47.117.36.70/',
        timeout: 10000000
      })
      const res = await request({
        // url: '/admin/material/export_imgs/',
        url: `/platform/downResultExcelFile`,
        method: 'get',
        params,
        responseType: 'blob',
        // onDownloadProgress: (progressEvent) => {
        //   const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total)
        //   this.percent = percentCompleted
        // }
      })
      console.log('res==888', res);
      const url = window.URL.createObjectURL(new Blob([res.data]))
      const link = document.createElement('a')
      link.href = url
      link.setAttribute('download', fileName)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
      window.URL.revokeObjectURL(url)
      // const xlsxName = window.decodeURI(res.headers['content-disposition'].split('=')[1])
      // const fileName = xlsxName || `${filename}.zip`
      // if (res) {
      //   const blob = new Blob([res.data], { type: 'charset=utf-8' })
      //   const elink = document.createElement('a')
      //   elink.download = fileName
      //   elink.style.display = 'none'
      //   elink.href = URL.createObjectURL(blob)
      //   document.body.appendChild(elink)
      //   elink.click()
      //   URL.revokeObjectURL(elink.href) // 释放URL 对象0
      //   document.body.removeChild(elink)
      // }
      // responseMessage(res)
    },
    /* 物资管理 */

    //添加物资信息
    async addMaterialHandler(addForm) {
      const res = await addMaterial(addForm)
      responseMessage(res)
    },
    //编辑物资信息
    async editMaterialHandler(addForm) {
      const res = await editMaterial(addForm)
      responseMessage(res)
    },
    //删除物资信息
    async delMaterialHandler(id) {
      const res = await delMaterial(id)
      responseMessage(res)
    },
    async batchDelMaterialHandler(ids) {
      const res = await request.post('/admin/material/batch_delete/', {
        ids: ids
      })
      responseMessage(res)
    },
    async removeBatchMaterial(ids) {
      const res = await request({
        url: 'admin/excel/model',
        method: 'delete',
        data,
      })
      responseMessage(res)
    },
    async export_excelHandler(params, warehouse_name) {
      const res = await export_excel(params)
      downloadFile(res, 'excel')
    },

    async export_excelModel(params) {
      const res = await request({
        url: 'admin/excel/model',
        method: 'get',
        params,
        responseType: 'blob'
      })
      downloadFile(res, 'excel')
    }
  }
})

export default useApiStore
